package com.texplose.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.texplose.entity.vo.*;
import com.texplose.service.RecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : lyj
 * @Timer : 2022/5/18
 * @Description :
 */
@RestController
@RequestMapping("/recipe")
public class RecipeController {
    @Autowired
    private RecipeService recipeService;

    @GetMapping("/getWorksByUserId/{userId}")
    public ResponseResult getWorksByUserId(@PathVariable("userId") int userId){
        return recipeService.getWorksByUserId(userId);
    }
    @GetMapping("/getRecipeListByCategoryId/{id}")
    public ResponseResult getRecipeListByCategoryId(@PathVariable("id")Integer id){
        return recipeService.getRecipeListByCategoryId(id);
    }


    @GetMapping("/getSwiper")
    public ResponseResult getSwiper(){
        return recipeService.getSwiper();
    }

    @GetMapping("/getIndexCards")
    public ResponseResult getIndexCards(){
        List<IndexCardsVo> indexCardsVos = recipeService.getIndexCards();
        return ResponseResult.okResult(indexCardsVos);
    }

    @GetMapping("/getIndexCardsByCreate")
    public ResponseResult getIndexCardsByCreate(){
        List<IndexCardsVo> indexCardsVos = recipeService.getIndexCardsByCreate();
        return ResponseResult.okResult(indexCardsVos);
    }

    @GetMapping("/getIndexCardsByLikes")
    public ResponseResult getIndexCardsByLikes(){
        List<IndexCardsVo> indexCardsVos = recipeService.getIndexCardsByLikes();
        return ResponseResult.okResult(indexCardsVos);
    }

    /**
     * 根据用户Id查询用户收藏的作品
     * @param id
     * @return
     */
    @GetMapping("/getCollectionsByUserId/{id}")
    public ResponseResult getCollectionsByUserId(@PathVariable("id") Integer id){
        List<UserCollectionVo> userCollectionVos = recipeService.getCollectionsByUserId(id);
        return ResponseResult.okResult(userCollectionVos);
    }

    /**
     * 删除作品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteRecipeById(@PathVariable("id") Integer id){
        recipeService.deleteRecipeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 根据recipeId查询食谱详情
     * @param id
     * @return
     */
    @GetMapping("/getRecipeDetailById/{id}")
    public ResponseResult getRecipeDetailById(@PathVariable("id") Integer id){
        RecipeDetailVo recipeDetailVo = recipeService.getRecipeDetailById(id);
        return ResponseResult.okResult(recipeDetailVo);
    }

    /**
     * 根据title模糊查询
     * @param title
     * @return
     */
    @GetMapping("/searchRecipeByTitle")
    public ResponseResult searchRecipeByTitle(String title){
        List<IndexCardsVo> indexCardsVo  = recipeService.searchRecipeByTitle(title);
        PageInfo pageInfo = new PageInfo(indexCardsVo);
        return ResponseResult.okResult(pageInfo);
    }

    @PutMapping("/updateView/{recipeId}")
    public ResponseResult updateView(@PathVariable("recipeId") Integer recipeId){
        return recipeService.updateView(recipeId);
    }

    @PutMapping("/addRecipe")
    public ResponseResult addRecipe(@RequestBody AddRecipeVo addRecipeVo){
        return recipeService.addRecipe(addRecipeVo);
    }



    @GetMapping("/countByUserId/{id}")
    public ResponseResult countByUserId(@PathVariable("id") Integer id){
        int totalCount = recipeService.countByUserId(id);
        return ResponseResult.okResult(totalCount);
    }

    @GetMapping("/countAll")
    public ResponseResult countAll(){

        return ResponseResult.okResult();
    }

}
